<template>
  <div class="divBox">
    <div class="selCard">
      <el-form inline size="small" :model="tableFrom" ref="searchForm" label-width="85px">

        <el-form-item label="所属酒店：" prop="hotel_id">
          <el-select
            v-model="tableFrom.hotel_id"
            filterable
            remote
            reserve-keyword
            placeholder="请选择酒店"
            :remote-method="remoteMethod"
            :loading="hotelLoading">
            <el-option
              v-for="item in hotelOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示状态：" prop="is_show">
          <el-select v-model="tableFrom.is_show" clearable filterable placeholder="请选择" class="selWidth" @change="getList(1)">
            <el-option label="显示" :value="1" />
            <el-option label="不显示" :value="0" />
          </el-select>
        </el-form-item>
    
        <el-form-item label="搜索：" prop="keyword">
          <el-input v-model="tableFrom.keyword" @keyup.enter.native="getList(1)" placeholder="请输入名称" class="selWidth" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <router-link    :to="{path: roterPre + '/product/hotel/addRoomType'}" class="mr10 ">
        <el-button size="small" type="primary" class="mb20">添加酒店房型</el-button>
      </router-link>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
      >
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column  key="20" label="酒店名称" min-width="100">
          <template slot-scope="scope">
            <div class="demo-image__preview">
             <span>{{scope.row.hotel?scope.row.hotel.name:''}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="房型名称" min-width="120" />
   
        <el-table-column  key="20" label="封面图" min-width="100">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image style="width: 36px; height: 36px" :src="scope.row.slider_image[0]" :preview-src-list="[scope.row.slider_image]" />
            </div>
          </template>
        </el-table-column>

       
        <el-table-column prop="sort" align="center" label="排序" min-width="120">
          <template slot-scope="scope">
            <span v-if="scope.$index === tabClickIndex">
              <el-input v-model.number="scope.row['sort']" type="number" maxlength="300" size="mini" autofocus @blur="inputBlur(scope)" />
            </span>
            <span v-else @click.stop="tabOneClick" @dblclick.stop="tabClick(scope.$index)" style="cursor: pointer;">{{ scope.row['sort'] }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="is_show" label="是否显示" min-width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              :active-value="1"
              :inactive-value="0"
              active-text="显示"
              inactive-text="隐藏"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column prop="create_time" label="创建时间" min-width="100" /> -->
        <el-table-column label="操作" min-width="80" fixed="right">
          <template slot-scope="scope">
            <router-link v-if="tableFrom.type != 5" :to="{path: roterPre + '/product/hotel/addRoomType/'+ scope.row.id}" >
              <el-button type="text" size="small" class="mr10">编辑</el-button>
            </router-link>
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
            >删除</el-button>
          </template>
          
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div> 
    </el-card>

  </div>
</template>

<script>
import { roterPre } from "@/settings";

import ueditorFrom from '@/components/ueditorFrom'
import {
hotelLstApi,  hotelRoomTypeLstApi,hotelRoomTypeSetStatusApi,hotelRoomTypeDeleteApi,hotelRoomTypeSortApiSortApi
} from "@/api/product";
export default {
  name: "LabelList",
  components: {
    ueditorFrom,
  },
  data() {
    return {
      hotelOptions: [],
      hotelLoading: false,
      roterPre:roterPre,
      tabClickIndex: '',
      listLoading: true,
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        hotel_id:'',
        is_show: 1,
        keyword: '',
        page: 1,
        limit: 20,
      },
      rule_id: '',
      merCateList: [],
      dialogVisible: false,
      fullscreenLoading: false,
      formValidate: {},
      ruleValidate: {
        rule_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "change" }],
        content: [{ required: true, message: "请输入说明详情", trigger: "blur" }],
      }
    };
  },
  mounted() {
    this.getList('');
  },
  methods: { 
    /**重置 */
    searchReset(){
      this.$refs.searchForm.resetFields();
      this.getList(1);
    }, 
    remoteMethod(query){
        if (query !== '') {
          this.hotelLoading = true;

          hotelLstApi({ keyword: query })
          .then(res => {
            this.hotelLoading = false;
            this.hotelOptions = res.data.list;
          })
          .catch(res => {
            this.$message.error(res.message)
          })

            
        } else {
          this.hotelOptions = [];
        }
    },
   
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      hotelRoomTypeLstApi(this.tableFrom)
        .then((res) => {
      
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;  
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList('');
    },
     // 添加明细原因 row 当前行 column 当前列
    tabClick(index) {
      this.tabClickIndex = index
    },
    tabOneClick(){
       this.tabClickIndex ='';
    },
    // 失去焦点初始化
    inputBlur(scope) {
      if (!scope.row.sort || scope.row.sort < 0) scope.row.sort = 0
      hotelRoomTypeSortApiSortApi(scope.row.id, { sort: scope.row.sort })
        .then((res) => {
          this.closeEdit()
        })
        .catch((res) => {
        })
    },
    closeEdit() {
      this.tabClickIndex = null
    },

    // 添加
    onAdd() {
      this.rule_id = "";
      this.dialogVisible = true;
      this.reset()
    },
    // 编辑
    onEdit(row) {
      this.rule_id = row.rule_id;
      this.formValidate = row;
      this.dialogVisible = true;
    },
    reset() {
      this.formValidate = {
        cate_id: [],
        rule_name: '',
        content: '',
        is_show: 0,
        sort: ''
      }
    },
  
    onchangeIsShow(row) {
      hotelRoomTypeSetStatusApi(row.id,  row.is_show ? 1 : 0 )
        .then(({ res }) => {
          // this.$message.success(res.msg);
          this.getList('');
        })
        .catch(({ message }) => {
          this.$message.error(msg);
        });
    },

   
      // 删除
      handleDelete(id, idx) {
      this.$modalSure('确定删除?').then(() => {
        hotelRoomTypeDeleteApi(id)
          .then(({ message }) => {
            // this.$message.success(message);
            this.getList('');
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
   
  },
};
</script>

<style scoped lang="scss">
</style>
